package com.hna.face.controller.business;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.face.common.result.BaseResult;
import com.hna.face.model.IotDevice;
import com.hna.face.model.IotDeviceVo;
import com.hna.face.model.PageInfo;
import com.hna.face.service.IIotDeviceService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 设备监控
 * 
 * @author Administrator
 *
 */

@Controller
public class DeviceController {

	@Autowired
	private IIotDeviceService iIotDeviceService;
	
    private final static Logger log = LoggerFactory.getLogger(DeviceController.class);


	// 跳device主页面
	@RequestMapping("/device/index")
	private String index() {
		return "pages/device/index";
	}

	// 跳device add页面
	@RequestMapping("/device/add")
	private String add() {
		return "pages/device/add";
	}

	// 跳device update页面,根据ID查找
	@RequestMapping("/device/selectId")
	private String sselectId(Long id, Model model) {
		EntityWrapper<IotDevice> wrapper = new EntityWrapper<>();
		wrapper.eq("id", id);
		model.addAttribute("device", iIotDeviceService.selectOne(wrapper));
		return "pages/device/edit";
	}

	// device主页
	@RequestMapping(value = "/device/list", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> deviceList(PageInfo pageInfo, IotDeviceVo deviceVo) {

		Map<String, Object> res = new HashMap<>();
		try {
			EntityWrapper<IotDevice> wrapper = new EntityWrapper<>();
			if (StringUtils.isNotBlank(deviceVo.getName())) {
				wrapper.like("name", deviceVo.getName());
			}
			if (StringUtils.isNotBlank(deviceVo.getStatus())) {
				wrapper.eq("status", deviceVo.getStatus());
			}
			if (StringUtils.isNotBlank(deviceVo.getType())) {
				wrapper.eq("type", deviceVo.getType());
			}
			if (StringUtils.isNotBlank(deviceVo.getStartTime())) {
				wrapper.ge("borrow_begin_day", deviceVo.getStartTime());
			}
			if (StringUtils.isNotBlank(deviceVo.getEndTime())) {
				wrapper.le("borrow_begin_day", deviceVo.getEndTime());
			}
			if (StringUtils.isNotBlank(deviceVo.getPrice1())) {
				wrapper.ge("price", deviceVo.getPrice1());
			}
			if (StringUtils.isNotBlank(deviceVo.getPrice2())) {
				wrapper.le("price", deviceVo.getPrice2());
			}
			Page<IotDevice> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<IotDevice> selectPage = iIotDeviceService.selectPage(page, wrapper);
			res.put("rows", selectPage.getRecords());
			res.put("total", selectPage.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}

	// devicet add
	@RequestMapping(value = "/device/save", method = RequestMethod.POST)
	@ResponseBody
	private BaseResult save(IotDevice device) {
		try {
			device.setCreateTime(new Date());
			device.setStatus("1");
			iIotDeviceService.insert(device);
			return BaseResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return BaseResult.fail(e.getMessage());
		}
	}

	// devicet update
	@RequestMapping(value = "/device/update", method = RequestMethod.POST)
	@ResponseBody
	private BaseResult update(IotDevice device) {
		try {
			EntityWrapper<IotDevice> wrapper = new EntityWrapper<>();
			wrapper.eq("id", device.getId());
			device.setUpdateTime(new Date());
			iIotDeviceService.update(device, wrapper);
			return BaseResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return BaseResult.fail(e.getMessage());
		}
	}

	// devicet delete
	@RequestMapping(value = "/device/del", method = RequestMethod.POST)
	@ResponseBody
	private BaseResult del(String ids) {
		try {
        	if(StringUtils.isEmpty(ids)){
        		return BaseResult.success();
        	}
        	iIotDeviceService.deleteBatchIds(Arrays.asList(ids.split(",")));
             return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
	}

}
